package org.dev.vlp.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dev.vlp.dto.CircleData;
import org.dev.vlp.dto.ColumnData;
import org.dev.vlp.entity.DailyRecord;
import org.dev.vlp.mapper.LearnRecordMapper;
import org.dev.vlp.service.DailyRecordService;
import org.dev.vlp.mapper.DailyRecordMapper;
import org.dev.vlp.service.StatisticsService;
import org.dev.vlp.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 86184
* @description 针对表【vlp_daily_record】的数据库操作Service实现
* @createDate 2023-01-23 18:11:27
*/
@Service
public class DailyRecordServiceImpl extends ServiceImpl<DailyRecordMapper, DailyRecord>
    implements DailyRecordService{

    @Autowired
    private DailyRecordMapper dailyRecordMapper;

    @Autowired
    private StatisticsService statisticsService;

    @Autowired
    private LearnRecordMapper learnRecordMapper;

    public long dayDiff(Date date1, Date date2) {
        return (date2.getTime() - date1.getTime()) / 86400000;
    }


    /**
     * 根据用户ID从数据库查询该用户最近七天的单词数据
     * 并进行数据封装
     * @param
     * @return
     */
    public ColumnData findLatestSevenDaysWordData(int userId){
        int lan = statisticsService.getLan(userId);
        List<DailyRecord> records = dailyRecordMapper.findLatestSevenDaysData(userId,lan);
        ColumnData columnData = new ColumnData();
        List<ColumnData.TimeData> result1 = new ArrayList<>();
        List<ColumnData.TimeData> result2 = new ArrayList<>();
        Date todayDate = DateUtil.parseDate(DateUtil.today());
        DateTime beginDate = DateUtil.offsetDay(todayDate, -6);
        for(DailyRecord dateRecord : records){
            Date gmtCreate = dateRecord.getGmtCreate();
            if(gmtCreate!=beginDate){
                while(true){
                    if(!beginDate.equals(gmtCreate)){
                        ColumnData.TimeData data1 = new ColumnData.TimeData();
                        data1.setDate(beginDate);
                        data1.setValue(0);
                        result1.add(data1);
                        ColumnData.TimeData data2= BeanCopyUtils.copy(
                                data1,ColumnData.TimeData.class);
                        result2.add(data2);
                        beginDate=DateUtil.offsetDay(beginDate, 1);
                    }else{
                        break;
                    }
                }
            }
            ColumnData.TimeData data1 = new ColumnData.TimeData();
            data1.setDate(gmtCreate);
            data1.setValue(dateRecord.getLearnCount());
            result1.add(data1);
            ColumnData.TimeData data2= BeanCopyUtils.copy(
                    data1,ColumnData.TimeData.class);
            data2.setValue(dateRecord.getReviewCount());
            result2.add(data2);
            beginDate= DateUtil.offsetDay(beginDate, 1);
        }
        while(!(beginDate.compareTo(todayDate) >0)){
            ColumnData.TimeData data1 = new ColumnData.TimeData();
            data1.setDate(beginDate);
            data1.setValue(0);
            result1.add(data1);
            ColumnData.TimeData data2= BeanCopyUtils.copy(
                    data1,ColumnData.TimeData.class);
            result2.add(data2);
            beginDate=DateUtil.offsetDay(beginDate, 1);
        }
        columnData.setLearn(result1);
        columnData.setReview(result2);
        return columnData;
    }

    /**
     * 根据用户ID从数据库查询已学单词各阶段的百分比
     * 并进行数据封装
     * @param userID
     * @return
     */
    public List<CircleData> findMmryPercentage(int userID){
        int lan = statisticsService.getLan(userID);
        List<CircleData> records = learnRecordMapper.findMmryPercentage(userID,lan);
        for(int i = 0;i<records.size();i++){
            switch (records.get(i).getPhase()){
                case 1:records.get(i).setType("认识");break;
                case 2:records.get(i).setType("了解");break;
                case 3:records.get(i).setType("熟悉");break;
                case 4:records.get(i).setType("掌握");break;
                case 5:records.get(i).setType("牢记");break;
                case 6:records.get(i).setType(("烂熟于心"));break;
            }
        }
        return records;
    }

}




